package com.ordos.recruit.module.audit.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ordos.recruit.module.apply.entity.RecruitApplyPo;
import com.ordos.recruit.module.apply.mapper.RecruitApplyMapper;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditDetailDto;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditDetailPo;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditDto;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditPo;
import com.ordos.recruit.module.audit.mapper.ApplyAuditMapper;
import com.ordos.recruit.module.audit.mapper.ApplyAuditsMapper;
import com.ordos.recruit.module.user.entity.SysSsoUserDto;
import com.ordos.recruit.module.user.util.UserUtil;
import com.wonders.commons.util.exception.ApiException;
import com.wonders.commons.util.response.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Service
 *
 * @author jin
 */
@Service
public class ApplyAuditService {

    private final ApplyAuditMapper applyAuditMapper;
    private final ApplyAuditsMapper applyAuditsMapper;
    private final RecruitApplyMapper recruitApplyMapper;

    public ApplyAuditService(ApplyAuditMapper applyAuditMapper, ApplyAuditsMapper applyAuditsMapper, RecruitApplyMapper recruitApplyMapper) {
        this.applyAuditMapper = applyAuditMapper;
        this.applyAuditsMapper = applyAuditsMapper;
        this.recruitApplyMapper = recruitApplyMapper;
    }


    /**
     * 保存审核信息。
     * <p>
     *     若id为空时,代表是新增.
     *     若id为非空时, 先删除子表数据, 在新增子表数据, 在Update主表数据。
     * </p>
     *
     * @param auditDto auditDto
     */
    public ApiResponse<String> saveOrUpdateAudit(RecruitmentApplicationAuditDto auditDto) {
        SysSsoUserDto sysSsoUserDto = UserUtil.getUserByRequest();
        RecruitApplyPo recruitApplyPo = recruitApplyMapper.selectById(auditDto.getApplicationId());
        if(recruitApplyPo == null){
            return ApiResponse.error("招聘申请id传输错误，未找到对应的招聘申请");
        }
        RecruitmentApplicationAuditPo auditPo = new RecruitmentApplicationAuditPo();
        if (StringUtils.isBlank(auditDto.getId())) {
            String applicationAuditId = UUID.randomUUID().toString().replace("-", "");
            BeanUtil.copyProperties(auditDto, auditPo);
            auditPo.setId(applicationAuditId);
            auditPo.setCreateUser(sysSsoUserDto.getUserid());
            auditPo.setUpdateTime(new Date());
            auditPo.setUpdateUser(sysSsoUserDto.getUserid());
            applyAuditMapper.insert(auditPo);

            List<RecruitmentApplicationAuditDetailDto> auditDetailDtoList = auditDto.getAuditDetailList();
            RecruitmentApplicationAuditDetailPo auditDetailDtoPo = null;
            RecruitmentApplicationAuditDetailDto auditDetailDtoDto = null;
            for (RecruitmentApplicationAuditDetailDto experienceDto : auditDetailDtoList) {
                String auditDetailId = UUID.randomUUID().toString().replace("-", "");
                auditDetailDtoDto = experienceDto;
                auditDetailDtoPo = new RecruitmentApplicationAuditDetailPo();
                BeanUtil.copyProperties(auditDetailDtoDto, auditDetailDtoPo);
                auditDetailDtoPo.setId(auditDetailId);
                auditDetailDtoPo.setAuditId(applicationAuditId);
                auditDetailDtoPo.setCreateUser(sysSsoUserDto.getUserid());
                auditDetailDtoPo.setUpdateTime(new Date());
                auditDetailDtoPo.setUpdateUser(sysSsoUserDto.getUserid());
                applyAuditsMapper.insert(auditDetailDtoPo);
            }
        }else{
            BeanUtil.copyProperties(auditDto, auditPo);
            auditPo.setUpdateTime(new Date());
            auditPo.setUpdateUser(sysSsoUserDto.getUserid());
            applyAuditMapper.updateById(auditPo);

            QueryWrapper<RecruitmentApplicationAuditDetailPo> auditDetailQuery = new QueryWrapper<>();
            auditDetailQuery.eq("AUDIT_ID", auditPo.getId());
            applyAuditsMapper.delete(auditDetailQuery);
            List<RecruitmentApplicationAuditDetailDto> auditDetailDtoList = auditDto.getAuditDetailList();
            RecruitmentApplicationAuditDetailPo auditDetailDtoPo = null;
            RecruitmentApplicationAuditDetailDto auditDetailDtoDto = null;
            for (RecruitmentApplicationAuditDetailDto experienceDto : auditDetailDtoList) {
                String auditDetailId = UUID.randomUUID().toString().replace("-", "");
                auditDetailDtoDto = experienceDto;
                auditDetailDtoPo = new RecruitmentApplicationAuditDetailPo();
                BeanUtil.copyProperties(auditDetailDtoDto, auditDetailDtoPo);
                auditDetailDtoPo.setId(auditDetailId);
                auditDetailDtoPo.setAuditId(auditPo.getId());
                auditDetailDtoPo.setCreateUser(sysSsoUserDto.getUserid());
                auditDetailDtoPo.setUpdateTime(new Date());
                auditDetailDtoPo.setUpdateUser(sysSsoUserDto.getUserid());
                applyAuditsMapper.insert(auditDetailDtoPo);
            }
        }
        return ApiResponse.success();
    }

    /**
     * 查看审核信息.
     *
     * @param id id
     * @return RecruitmentApplicationAuditPo
     */
    public RecruitmentApplicationAuditPo allView(String id) {
        if (CharSequenceUtil.isBlank(id)) {
            throw new ApiException("id不能为空.");
        }

        RecruitmentApplicationAuditPo auditPo = applyAuditMapper.selectById(id);

        QueryWrapper<RecruitmentApplicationAuditDetailPo> auditDetailQuery = new QueryWrapper<>();
        auditDetailQuery.eq("AUDIT_ID", auditPo.getId());
        List<RecruitmentApplicationAuditDetailPo> auditDetailList = applyAuditsMapper.selectList(auditDetailQuery);
        auditPo.setAuditDetailList(auditDetailList);

        return auditPo;
    }
}
